<?php
class Administrator_album_model extends CI_Model {

    function __construct()
    {
        parent::__construct();
    }	
	
	function content_management_add()
	{
		$is_publish = $this->input->post('is_publish');
		$label_id = $this->input->post('label_id');
		$album_release_date = $this->input->post('album_release_date');
		$is_show_release_date = $this->input->post('is_show_release_date');
		$is_show_recommend = $this->input->post('is_show_recommend');
		$album_price = $this->input->post('album_price');
		$letters = $this->input->post('letters');
		$album_name = $this->input->post('album_name');
		$album_description = $this->input->post('album_description');
		$album_tag = $this->input->post('album_tag');
		$song_id = $this->input->post('song_id');
		$uploadify_store = explode(',',$this->input->post('uploadify_store'));
		$datetime = date('Y-m-d H:i:s');
		
		//Insert album
		$data = array(
			'label_id' => $label_id,
			'is_publish' => $is_publish,
			'is_show_recommend' => $is_show_recommend,
			'album_price' => $album_price,
			'letters' => $letters,
			'update_by' => $this->session->userdata('username'),
			'create_date' => $datetime,
			'update_date' => $datetime
		);
		$this->db->insert('album',$data);
		$album_id = $this->db->insert_id();
		
		//Insert album_info
		$data = array(
			'album_id' => $album_id,
			'album_name' => $album_name,
			'album_description' => $album_description,
			'is_show_release_date' => $is_show_release_date,			
			'album_release_date' => convertDatetime($album_release_date),			
			'album_tag' => $album_tag
		);
		$this->db->insert('album_info',$data);	
		
		//Update album_image
		if(!empty($uploadify_store[0])) foreach($uploadify_store as $key => $value):
			//Resize Image
			$data = imageResize(array(
				'id' => $album_id,
				'source_image' => $this->temp_folder.$value,
				'new_path' => $this->upload_folder.'album/images/',
				'dimension' => array(
					array(
						'width' => 120,
						'height' => 120
					)				
				)
			));		
			
			//delete temp
			@unlink($this->temp_folder.$value);
			
			$dataImage = array(
				'album_id' => $album_id,
				'album_file_name' => $data->file_name,
				'album_file_ext' => $data->file_ext
			);	
		endforeach;
		if(!empty($dataImage)) $this->db->update('album_info',$dataImage,array('album_id' => $album_id));

		//Update Solr
		$this->load->library('solr');	
			
		$document = new Apache_Solr_Document();
		$document->id = 'album_'.$album_id;
		$document->album_name_s = $album_name;
		$document->album_tag_s = $album_tag;
		$document->last_modified = date("Y-m-d\TH:i:s\Z",strtotime($datetime));	
		if(!empty($dataImage)):
			$document->album_image_name_s = $dataImage['album_file_name'].'.'.$dataImage['album_file_ext'];
		endif;
		
		if($this->solr->ping() && !empty($document)):
			$this->solr->addDocument($document);
			$this->solr->commit(); //commit to see the deletes and the document
			$this->solr->optimize(); //merges multiple segments into one
		endif;	
		
		//Delete Memcache
		$this->memcache->delete('dataAlbumReview');	
	}
	
	function content_management_edit($album_id)
	{
		$is_publish = $this->input->post('is_publish');
		$label_id = $this->input->post('label_id');
		$album_release_date = $this->input->post('album_release_date');
		$is_show_release_date = $this->input->post('is_show_release_date');
		$is_show_recommend = $this->input->post('is_show_recommend');
		$album_price = $this->input->post('album_price');
		$letters = $this->input->post('letters');
		$album_name = $this->input->post('album_name');
		$album_description = $this->input->post('album_description');
		$album_tag = $this->input->post('album_tag');
		$song_id = $this->input->post('song_id');
		$uploadify_store = explode(',',$this->input->post('uploadify_store'));
		$datetime = date('Y-m-d H:i:s');
		
		/*Update user*/
		$data = array(
			'label_id' => $label_id,
			'is_publish' => $is_publish,
			'is_show_recommend' => $is_show_recommend,
			'album_price' => $album_price,
			'letters' => $letters,
			'update_by' => $this->session->userdata('username'),
			'update_date' => $datetime
		);
		$this->db->update('album',$data,array('album_id' => $album_id));
		
		/*Update album_info*/
		$data = array(
			'album_id' => $album_id,
			'album_name' => $album_name,
			'album_description' => $album_description,
			'is_show_release_date' => $is_show_release_date,
			'album_release_date' => convertDatetime($album_release_date),			
			'album_tag' => $album_tag
		);
		$this->db->update('album_info',$data,array('album_id' => $album_id));
		
		//Update album_image
		if(!empty($uploadify_store[0])) foreach($uploadify_store as $key => $value):
			//Resize Image
			$data = imageResize(array(
				'id' => $album_id,
				'source_image' => $this->temp_folder.$value,
				'new_path' => $this->upload_folder.'album/images/',
				'dimension' => array(
					array(
						'width' => 120,
						'height' => 120
					)			
				)
			));		
			
			//Delete temp image
			@unlink($this->temp_folder.$value);
			$dataInfo = $this->db->get_where('album_info',array('album_id' => $album_id),1)->row();
			$map = directory_map($this->upload_folder.'album/images/', 1);
			foreach($map as $folder):
				@unlink($this->upload_folder.'album/images/'.$folder.'/'.$dataInfo->album_file_name.'.'.$dataInfo->album_file_ext);
			endforeach;
			
			$dataImage = array(
				'album_id' => $album_id,
				'album_file_name' => $data->file_name,
				'album_file_ext' => $data->file_ext
			);	
		endforeach;
		if(!empty($dataImage)) $this->db->update('album_info',$dataImage,array('album_id' => $album_id));
		
		//Update Solr
		$this->load->library('solr');	
			
		$document = new Apache_Solr_Document();
		$document->id = 'album_'.$album_id;
		$document->album_name_s = $album_name;
		$document->album_tag_s = $album_tag;
		$document->last_modified = date("Y-m-d\TH:i:s\Z",strtotime($datetime));	
		$dataImage = $this->db->select('album_file_name,album_file_ext')->get_where('album_info',array('album_id' => $album_id),1)->row();
		if(!empty($dataImage->album_file_name)) $document->album_image_name_s = $dataImage->album_file_name.'.'.$dataImage->album_file_ext;
		
		if($this->solr->ping() && !empty($document)):
			$this->solr->addDocument($document);
			$this->solr->commit(); //commit to see the deletes and the document
			$this->solr->optimize(); //merges multiple segments into one
		endif;
		
		//Delete Memcache
		$this->memcache->delete('dataAlbumReview');							
	}	
	
	function content_management_delete()
	{
		$id = $this->input->post('id');
		$map = directory_map($this->upload_folder.'album/images/', 1);
		$ids = array();
		foreach($id as $value):
			$ids[] = "album_".$value;
			$dataInfo = $this->db->get_where('album_info',array('album_id' => $value),1)->row();
			$tables = array('album','album_info','song_album');
			$this->db->where('album_id', $value);
			$this->db->delete($tables);
			//Delete All Image
			foreach($map as $folder):
				@unlink($this->upload_folder.'album/images/'.$folder.'/'.$dataInfo->album_file_name.'.'.$dataInfo->album_file_ext);
			endforeach;					
		endforeach;	
		
		//Update Solr
		$this->load->library('solr');	
		if($this->solr->ping() && !empty($ids)):
			$this->solr->deleteByMultipleIds($ids);
			$this->solr->commit(); //commit to see the deletes and the document
			$this->solr->optimize(); //merges multiple segments into one
		endif;
		
		//Delete Memcache
		$this->memcache->delete('dataAlbumReview');					
	}
}

?>